package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;
    @PostMapping
    public JSONResult login(User user){
        JSONResult result = new JSONResult();
        try {
            User temp = userService.login(user);
            //将当前登陆成功的用户加入到JSON中返回到页面 渲染使用
            result.setResult(temp);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @DeleteMapping
    public void logout(HttpSession session){
        session.invalidate();
    }

    /**
     * 人脸识别登录
     * @return
     */
    @GetMapping
    public JSONResult loginByAI(){
        User user = new User();
        user.setEmail("chenhui@wolfcode.cn");
        user.setPassword("1");
        return login(user);
    }
}
